package com.game.dao.user.task;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.task.TaskCfg;
import com.coment.po.UserTaskInfo;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public interface UserTaskInfoDao extends MongoRepository<UserTaskInfo, Integer> {
    default UserTaskInfo findUserTaskInfoByUserInfoIdAndTaskId(Integer userInfoId, Integer taskId) {
        return findById(Objects.hash(userInfoId, taskId)).orElseGet(() -> save(new UserTaskInfo(userInfoId, taskId)));
    }

    default List<UserTaskInfo> obtainByUserInfoId(Integer userInfoId) {
        List<UserTaskInfo> userTaskInfoList = new ArrayList<>();
        for (TaskCfg taskCfg : Tables.cfg.get_tbtaskcfg().get_dataList()) {
            UserTaskInfo userTaskInfo = findUserTaskInfoByUserInfoIdAndTaskId(userInfoId, taskCfg.id);
            userTaskInfoList.add(userTaskInfo);
        }
        return userTaskInfoList;
    }

    List<UserTaskInfo> findByUserInfoId(Integer userInfoId);
}
